<?php

namespace app\common\model\mall\goods;

use think\Model;

class Catattr extends Model
{
    // 表名
    protected $name = 'mall_goods_catattr';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'attrtype_text',
        'cat_id_text'
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }
    
    public function getAttrtypeList()
    {
        return ['0' => __('Attrtype 0'),'1' => __('Attrtype 1'),'2' => __('Attrtype 2')];
    }     


    public function getAttrtypeTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['attrtype']) ? $data['attrtype'] : '');
        $list = $this->getAttrtypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCatIdTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cat_id']) ? $data['cat_id'] : '');
        $names = model("\app\common\model\mall\goods\Cat")->getParentNames($value);
        return $names ? implode("/",$names) : '';
    }

    public function cat()
    {
        return $this->belongsTo('Cat', 'cat_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
